package cn.sugela.aichat.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * @author ryan
 * @date 2024-11-15 13:33:47
 */
@RestController
@RequestMapping("/v1/video/play")
public class VideoController {

    @RequestMapping("/play_info")
    public  ResponseEntity<String> getPlayInfo() {
        return null;
    }

    @RequestMapping("/play/hls_key")
    public ResponseEntity<byte[]> getPlayKey(@RequestParam(name = "id", defaultValue = "6558") Integer id, HttpServletResponse response) {
        //生成加密的密钥
        String key = "";
        int size = 16 - key.length();
        String hlsKey = key + String.format("%0" + size + "d", id);
        if (hlsKey.length() > 16) {
            hlsKey = hlsKey.substring(0, 16);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/pgp-keys");
        headers.set("Content-disposition", "inline; filename=\"hls128.key\"; filename*=utf-8''hls128.key");
        headers.set("Content-Transfer-Encoding", "binary");

        return ResponseEntity.ok().headers(headers).body(hlsKey.getBytes(StandardCharsets.UTF_8));
    }

}
